<?php
namespace App\Http\Controllers\Customer;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// use Illuminate\Support\Facades\Hash;
class OrderController extends Controller{
	public function __construct()
    {
        $this->middleware('customer');
    }
    public function orders()
    {
        return view('customer.order.orders');
    }
    public function odata(Request $request){
        $key=$request->input('key');
        $state=$request->input('state');
        if (!empty($request->input('start_time'))) {
            $start_time=strtotime($request->input('start_time'));
        }else{
            $start_time=0;
        }
        if (!empty($request->input('end_time'))) {
            $end_time=strtotime($request->input('end_time'));
        }else{
            $end_time=time();
        }
        if($state==0){
            if(!empty($key)){
                $count = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`,`nickname`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->count();
            }else{
                $count = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->count();
            }
        }else{
            if(!empty($key)){
                $count = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`,`nickname`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.state',$state)
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->count();
            }else{
                $count = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.state',$state)
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->count();
            }
        }
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        if($state==0){
            if(!empty($key)){
                $orders = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`,`nickname`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->orderBy('orders.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->get();
            }else{
                $orders = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->orderBy('orders.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->get();
            }
        }else{
            if(!empty($key)){
                $orders = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->whereRaw("concat(`course`,`account`,`teacher`,`nickname`) like '%".$key."%'")
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.state',$state)
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->orderBy('orders.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->get();
            }else{
                $orders = DB::table('orders')
                ->leftJoin('course', 'orders.course_id', '=', 'course.id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->leftJoin('teacher', 'course.t_id', '=', 'teacher.id')
                ->where('course.t_id',\Auth::guard('customer')->user()->toArray()['id'])
                ->where('orders.state',$state)
                ->where('orders.time','>',"{$start_time}")
                ->where('orders.time','<',"{$end_time}")
                ->orderBy('orders.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('orders.*', 'course.course', 'users.nickname', 'teacher.teacher', 'teacher.account')
                ->get();
            }
        }
        foreach ($orders as $k=>$item){
            $orders[$k]->time =date('Y-m-d H:i:s',$orders[$k]->time);
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $count,
            'data'  => $orders
        ];
        return response()->json($data);
    }
}